package com.itheima.mq;

import com.itheima.domain.mongo.MovementScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

//mq消息监听,监听tanhuan-app发来的消息,进行分数计算
@Component
@RocketMQMessageListener(topic = "tanhua-recommend-161", consumerGroup = "recommend")
public class MovementScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    //监听器的方法
    //- 浏览 +  1
    //- 点赞 +  5
    //- 喜欢 +  8
    //- 评论 + 10
    @Override
    public void onMessage(Map message) {
        System.out.println("开始计算:" + message);
        //获取传过来的参数
        Long userId = Long.valueOf(message.get("userId").toString());
        Long pid = Long.valueOf(message.get("pid").toString());
        Integer type = Integer.parseInt(message.get("type").toString());

        //构建动态评分对象
        MovementScore movementScore = new MovementScore();
        movementScore.setDate(System.currentTimeMillis());
        movementScore.setUserId(userId);
        movementScore.setPublishId(pid);
        switch (type) {
            case 1:
                movementScore.setScore(20d);
                break;
            case 2:
                movementScore.setScore(1d);
                break;
            case 3:
                movementScore.setScore(5d);
                break;
            case 4:
                movementScore.setScore(8d);
                break;
            case 5:
                movementScore.setScore(10d);
                break;
            case 6:
                movementScore.setScore(-5d);
                break;
            case 7:
                movementScore.setScore(-8d);
                break;
        }
        //将评分保存到mongo数据库中
        mongoTemplate.save(movementScore);
    }
}
